var FacilityNew = FacilityNew || {};

FacilityNew = function () {
 
  this.facMesh=null;
  this.isBind=false;
  this.goodsSession={
  	rndId:null,gid:null,
	position:{x:0,y:0,z:0},
  	rotation:{x:0,y:0,z:0},
  	size:{x:0,y:0,z:0}};
  this.goodsInfo=null;
  this.mouse2D= new THREE.Vector3( 0, 0, 0);
  this.centerOffset=new THREE.Vector3(0,0,0);
  this.scene=null;
  this.camera=null;
  this.keyFront=false;
  this.keyBack=false;
  this.keyRight=false;
  this.keyLeft=false;
  this.keyUp=false;
  this.keyDown=false;
  this.keySpace=false;
  
  this.parasitizedPoint=new THREE.Vector3;
  
  this.release=function(){
    this.bind=false;
    this.facMesh.type='released';
    this.goodsSession.position.x=this.facMesh.position.x;
    this.goodsSession.position.y=this.facMesh.position.y;
    this.goodsSession.position.z=this.facMesh.position.z;
    this.goodsSession.rotation.x=this.facMesh.rotation.x;
    this.goodsSession.rotation.y=this.facMesh.rotation.y;
    this.goodsSession.rotation.z=this.facMesh.rotation.z;
    this.goodsSession.size.x=this.facMesh.size.x;
    this.goodsSession.size.y=this.facMesh.size.y;
    this.goodsSession.size.z=this.facMesh.size.z;
    this.goodsSession.parasitizedFace=this.facMesh.parasitizedFace;
   
    // if(houseSession){
      // houseSession.push({gid:this.goodsSession.gid,
                // position:{x:this.facMesh.position.x,y:this.facMesh.position.y,z:this.facMesh.position.z},
                // rotation:{x:this.facMesh.rotation.x,y:this.facMesh.rotation.y,z:this.facMesh.rotation.z},
                // size:{x:this.facMesh.size.x,y:this.facMesh.size.y,z:this.facMesh.size.z}
      // });
    // }
    this.facMesh=null;
  };
  
   this.checkIntersect=function(tmpPoint,ctrSize,intersector){
      // I.x.Max=Min(C.Max.x,S.Max.x) I.x.Min=Max(C.Min.x,S.Min.x)   
      
//                            
		// if(tmpPoint.x+ctrSize.x/2>intersector.object.position.x+intersector.object.size.x/2 ||
		   // tmpPoint.x-ctrSize.x/2<intersector.object.position.x-intersector.object.size.x/2 ||
		   // tmpPoint.y+ctrSize.y/2>intersector.object.position.y+intersector.object.size.y/2 ||
		   // tmpPoint.y-ctrSize.y/2<intersector.object.position.y-intersector.object.size.y/2 ||
		   // tmpPoint.z+ctrSize.z/2>intersector.object.position.z+intersector.object.size.z/2 ||
		   // tmpPoint.z-ctrSize.z/2<intersector.object.position.z-intersector.object.size.z/2
		  // ){
			// return true;
			// }
      var ctrMax=new THREE.Vector3(ctrSize.x/2+tmpPoint.x,ctrSize.y/2+tmpPoint.y,ctrSize.z/2+tmpPoint.z);
            var ctrMin=new THREE.Vector3(tmpPoint.x-ctrSize.x/2,tmpPoint.y-ctrSize.y/2,tmpPoint.z-ctrSize.z/2);
            for(var i in this.scene.children){
                var g=this.scene.children[i];
            if( g instanceof THREE.Mesh && g!=intersector.object && g.type!='fac' && g.type!='background' && g.type!='cursor' ){
                    var gMax=new THREE.Vector3(g.position.x+g.size.x/2,g.position.y+g.size.y/2,g.position.z+g.size.z/2);
                    var gMin=new THREE.Vector3(g.position.x-g.size.x/2,g.position.y-g.size.y/2,g.position.z-g.size.z/2);
                    if(Math.min(ctrMax.x,gMax.x)>Math.max(ctrMin.x,gMin.x) && 
                       Math.min(ctrMax.y,gMax.y)>Math.max(ctrMin.y,gMin.y) && 
                       Math.min(ctrMax.z,gMax.z)>Math.max(ctrMin.z,gMin.z)) 
                   {
                    // planeGeo=new THREE.CubeGeometry( g.size.x+2,g.size.y+2,g.size.z+2);
                    // planeMat=new THREE.MeshBasicMaterial( { color: 0x000000 } );
                    // this.addMesh(planeGeo,1,g.position.x,g.position.y,g.position.z,0,0,0,planeMat,'tmp');
                    return true;
                    }
                }}
      
        return false;
      };
      
	this.addMesh=function(geometry, scale, x, y, z, rx, ry, rz, material, type) {
				geometry.computeBoundingBox();
				mesh = new THREE.Mesh(geometry, material);
				mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
				mesh.position.x = x;
				mesh.position.y = y;
				mesh.position.z = z;
				mesh.rotation.x = rx;
				mesh.rotation.y = ry;
				mesh.rotation.z = rz;
				mesh.overdraw = true;
				mesh.doubleSided = false;
				mesh.type = type;
				mesh.size = geometry.boundingBox.max.subSelf(geometry.boundingBox.min);
				mesh.updateMatrix();
				mesh.parasitizedFace={name:type,positionX:x,positionY:y+mesh.size.y/2,positionZ:z,sizeX:mesh.size.x,sizeY:0,sizeZ:mesh.size.z};
				this.scene.add(mesh);

				return mesh;

			}

};
FacilityNew.prototype = {

  constructor:FacilityNew,
  init:function(scene,camera){
    this.scene=scene;
    this.camera=camera;

  },
  loadCollada:function(goodsGeo,scene,goodsPos){
		var loader = new THREE.ColladaLoader();
		loader.load( './models/monster.dae', function colladaReady( collada ) {
		dae = collada.scene;
		skin = collada.skins[ 0 ];
		dae.scale.x = dae.scale.y = dae.scale.z = 0.002;
		dae.rotation.x = -Math.PI/2;
		dae.updateMatrix();

			} );
  },

	loadTextture:function(){
		
	},		
  load:function(goodsGeo,hostFacInfo,goodsPos){
    var me=this;
    var today=new Date();
    var rndId=Math.ceil((today.getTime()*9301+49297)%233280/233280.0*511555);

		
        var loader = new THREE.JSONLoader();
    loader.load( goodsGeo.geometry, function( geometry ) {
      me.goodsSession.gid=goodsGeo._id;
      var position=new THREE.Vector3(100,100,100);


      rollOverMaterial = new THREE.MeshBasicMaterial( { color: 0xFF99FF, opacity: 0.5, transparent: true } );
      matrials=new Array();
      matrials.push(new THREE.MeshFaceMaterial());
      matrials.push(rollOverMaterial);
      me.facMesh =me.addMesh(geometry, 1, position.x, position.y, position.z, 0, 0, 0, new THREE.MeshFaceMaterial(), 'fac');

           
      // rollOverGeo = new THREE.CubeGeometry( 80, 80, 80 );
      // rollOverMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.5, transparent: true } );            
      // me.facMesh =me.addMesh(rollOverGeo, 1, position.x, position.y, position.z, 0, 0, 0, rollOverMaterial, 'fac',goodsGeo.gTypeId);

      if(goodsPos){
		  position.set(parseInt(goodsPos.x),parseInt(goodsPos.y),parseInt(goodsPos.z));
          me.facMesh.name=hostFacInfo.rndId;
      }

      else{
        switch(hostFacInfo.parasitizedFace.name){
          case 'fwall':
            position.set(hostFacInfo.parasitizedFace.positionX,hostFacInfo.parasitizedFace.positionY,hostFacInfo.parasitizedFace.positionZ+me.facMesh.size.z/2);
            break;
          case 'bwall':
            position.set(hostFacInfo.parasitizedFace.positionX,hostFacInfo.parasitizedFace.positionY,hostFacInfo.parasitizedFace.positionZ-me.facMesh.size.z/2);
            break;
          case 'lwall':
            position.set(hostFacInfo.parasitizedFace.positionX+me.facMesh.size.x/2,hostFacInfo.parasitizedFace.positionY,hostFacInfo.parasitizedFace.positionZ);
            break;
          case 'rwall':
            position.set(hostFacInfo.parasitizedFace.positionX-me.facMesh.size.x/2,hostFacInfo.parasitizedFace.positionY,hostFacInfo.parasitizedFace.positionZ);
            break;
          case 'floor':
            position.set(hostFacInfo.parasitizedFace.positionX,hostFacInfo.parasitizedFace.positionY+me.facMesh.size.y/2,hostFacInfo.parasitizedFace.positionZ);
            break;
          case 'ceiling':
            position.set(hostFacInfo.parasitizedFace.positionX,hostFacInfo.parasitizedFace.positionY-me.facMesh.size.y/2,hostFacInfo.parasitizedFace.positionZ);
            break;
        }

      
      
      }
      me.facMesh.position.copy(position);
      me.facMesh._id=goodsGeo._id;

      
      me.facMesh.goodsName=goodsGeo.goodsName;
      me.facMesh.description=goodsGeo.description;
      me.facMesh.parasitizedFace=hostFacInfo.parasitizedFace;
      
      // if(goodsPos)me.release();
      
      });
      if(goodsPos){
          
          this.goodsSession.rndId=hostFacInfo.rndId;
          this.goodsInfo={name:hostFacInfo.rndId,type:'released',goodsName:goodsGeo.goodsName,description:goodsGeo.description,parasitizedFace:hostFacInfo.parasitizedFace};

      }
      else{
          this.goodsInfo={name:rndId,type:'fac',goodsName:goodsGeo.goodsName,description:goodsGeo.description,parasitizedFace:hostFacInfo.parasitizedFace};
          this.goodsSession.rndId=rndId;
      }

  },
  
  moveOnHost:function(mouse2D,hostFacInfo){
    // hostFacInfo=this.facMesh;
    this.locateOnHost(mouse2D,hostFacInfo);
if(this.parasitizedPoint){

    switch(this.facMesh.parasitizedFace.name){
          case 'fwall':
            this.parasitizedPoint.z=hostFacInfo.parasitizedFace.positionZ+this.facMesh.size.z/2;
            if(this.parasitizedPoint.x+this.facMesh.size.x/2>hostFacInfo.parasitizedFace.positionX+hostFacInfo.parasitizedFace.sizeX/2){
              this.parasitizedPoint.x=hostFacInfo.parasitizedFace.positionX+hostFacInfo.parasitizedFace.sizeX/2-this.facMesh.size.x/2;             
            }
            else if(this.parasitizedPoint.x-this.facMesh.size.x/2<hostFacInfo.parasitizedFace.positionX-hostFacInfo.parasitizedFace.sizeX/2){
              this.parasitizedPoint.x=hostFacInfo.parasitizedFace.positionX-hostFacInfo.parasitizedFace.sizeX/2+this.facMesh.size.x/2;
            }
            if(this.parasitizedPoint.y+this.facMesh.size.y/2>hostFacInfo.parasitizedFace.positionY+hostFacInfo.parasitizedFace.sizeY/2){
              this.parasitizedPoint.y=hostFacInfo.parasitizedFace.positionY+hostFacInfo.parasitizedFace.sizeY/2-this.facMesh.size.y/2;
            }
            else if(this.parasitizedPoint.y-this.facMesh.size.y/2<hostFacInfo.parasitizedFace.positionY-hostFacInfo.parasitizedFace.sizeY/2){
              this.parasitizedPoint.y=hostFacInfo.parasitizedFace.positionY-hostFacInfo.parasitizedFace.sizeY/2+this.facMesh.size.y/2;
            }
            // hasInsectorOnXY();
            break;
          case 'bwall':
            this.parasitizedPoint.z=hostFacInfo.parasitizedFace.positionZ-this.facMesh.size.z/2;
            if(this.parasitizedPoint.x+this.facMesh.size.x/2>hostFacInfo.parasitizedFace.positionX+hostFacInfo.parasitizedFace.sizeX/2){
              this.parasitizedPoint.x=hostFacInfo.parasitizedFace.positionX+hostFacInfo.parasitizedFace.sizeX/2-this.facMesh.size.x/2;
            }
            else if(this.parasitizedPoint.x-this.facMesh.size.x/2<hostFacInfo.parasitizedFace.positionX-hostFacInfo.parasitizedFace.sizeX/2){
              this.parasitizedPoint.x=hostFacInfo.parasitizedFace.positionX-hostFacInfo.parasitizedFace.sizeX/2+this.facMesh.size.x/2;
            }
            if(this.parasitizedPoint.y+this.facMesh.size.y/2>hostFacInfo.parasitizedFace.positionY+hostFacInfo.parasitizedFace.sizeY/2){
              this.parasitizedPoint.y=hostFacInfo.parasitizedFace.positionY+hostFacInfo.parasitizedFace.sizeY/2-this.facMesh.size.y/2;
            }
            else if(this.parasitizedPoint.y-this.facMesh.size.y/2<hostFacInfo.parasitizedFace.positionY-hostFacInfo.parasitizedFace.sizeY/2){
              this.parasitizedPoint.y=hostFacInfo.parasitizedFace.positionY-hostFacInfo.parasitizedFace.sizeY/2+this.facMesh.size.y/2;
            }
            break;
          case 'lwall':
            this.parasitizedPoint.x=hostFacInfo.parasitizedFace.positionX+this.facMesh.size.x/2;
            if(this.parasitizedPoint.z+this.facMesh.size.z/2>hostFacInfo.parasitizedFace.positionZ+hostFacInfo.parasitizedFace.sizeZ/2){
              this.parasitizedPoint.z=hostFacInfo.parasitizedFace.positionZ+hostFacInfo.parasitizedFace.sizeZ/2-this.facMesh.size.z/2;
            }
            else if(this.parasitizedPoint.z-this.facMesh.size.z/2<hostFacInfo.parasitizedFace.positionZ-hostFacInfo.parasitizedFace.sizeZ/2){
              this.parasitizedPoint.z=hostFacInfo.parasitizedFace.positionZ-hostFacInfo.parasitizedFace.sizeZ/2+this.facMesh.size.z/2;
            }
            if(this.parasitizedPoint.y+this.facMesh.size.y/2>hostFacInfo.parasitizedFace.positionY+hostFacInfo.parasitizedFace.sizeY/2){
              this.parasitizedPoint.y=hostFacInfo.parasitizedFace.positionY+hostFacInfo.parasitizedFace.sizeY/2-this.facMesh.size.y/2;
            }
            else if(this.parasitizedPoint.y-this.facMesh.size.y/2<hostFacInfo.parasitizedFace.positionY-hostFacInfo.parasitizedFace.sizeY/2){
              this.parasitizedPoint.y=hostFacInfo.parasitizedFace.positionY-hostFacInfo.parasitizedFace.sizeY/2+this.facMesh.size.y/2;
            }
            break;
          case 'rwall':
            this.parasitizedPoint.x=hostFacInfo.parasitizedFace.positionX-this.facMesh.size.x/2;
            if(this.parasitizedPoint.z+this.facMesh.size.z/2>hostFacInfo.parasitizedFace.positionZ+hostFacInfo.parasitizedFace.sizeZ/2){
              this.parasitizedPoint.z=hostFacInfo.parasitizedFace.positionZ+hostFacInfo.parasitizedFace.sizeZ/2-this.facMesh.size.z/2;
            }
            else if(this.parasitizedPoint.z-this.facMesh.size.z/2<hostFacInfo.parasitizedFace.positionZ-hostFacInfo.parasitizedFace.sizeZ/2){
              this.parasitizedPoint.z=hostFacInfo.parasitizedFace.positionZ-hostFacInfo.parasitizedFace.sizeZ/2+this.facMesh.size.z/2;
            }
            if(this.parasitizedPoint.y+this.facMesh.size.y/2>hostFacInfo.parasitizedFace.positionY+hostFacInfo.parasitizedFace.sizeY/2){
              this.parasitizedPoint.y=hostFacInfo.parasitizedFace.positionY+hostFacInfo.parasitizedFace.sizeY/2-this.facMesh.size.y/2;
            }
            else if(this.parasitizedPoint.y-this.facMesh.size.y/2<hostFacInfo.parasitizedFace.positionY-hostFacInfo.parasitizedFace.sizeY/2){
              this.parasitizedPoint.y=hostFacInfo.parasitizedFace.positionY-hostFacInfo.parasitizedFace.sizeY/2+this.facMesh.size.y/2;
            }
            break;
          case 'floor':
            this.parasitizedPoint.y=hostFacInfo.parasitizedFace.positionY+this.facMesh.size.y/2;
            if(this.parasitizedPoint.z+this.facMesh.size.z/2>hostFacInfo.parasitizedFace.positionZ+hostFacInfo.parasitizedFace.sizeZ/2){
              this.parasitizedPoint.z=hostFacInfo.parasitizedFace.positionZ+hostFacInfo.parasitizedFace.sizeZ/2-this.facMesh.size.z/2;
            }
            else if(this.parasitizedPoint.z-this.facMesh.size.z/2<hostFacInfo.parasitizedFace.positionZ-hostFacInfo.parasitizedFace.sizeZ/2){
              this.parasitizedPoint.z=hostFacInfo.parasitizedFace.positionZ-hostFacInfo.parasitizedFace.sizeZ/2+this.facMesh.size.z/2;
            }
            if(this.parasitizedPoint.x+this.facMesh.size.x/2>hostFacInfo.parasitizedFace.positionX+hostFacInfo.parasitizedFace.sizeX/2){
              this.parasitizedPoint.x=hostFacInfo.parasitizedFace.positionX+hostFacInfo.parasitizedFace.sizeX/2-this.facMesh.size.x/2;
            }
            else if(this.parasitizedPoint.x-this.facMesh.size.x/2<hostFacInfo.parasitizedFace.positionX-hostFacInfo.parasitizedFace.sizeX/2){
              this.parasitizedPoint.x=hostFacInfo.parasitizedFace.positionX-hostFacInfo.parasitizedFace.sizeX/2+this.facMesh.size.x/2;
            }
            break;
          case 'ceiling':
            this.parasitizedPoint.y=hostFacInfo.parasitizedFace.positionY-this.facMesh.size.y/2;
            if(this.parasitizedPoint.z+this.facMesh.size.z/2>hostFacInfo.parasitizedFace.positionZ+hostFacInfo.parasitizedFace.sizeZ/2){
              this.parasitizedPoint.z=hostFacInfo.parasitizedFace.positionZ+hostFacInfo.parasitizedFace.sizeZ/2-this.facMesh.size.z/2;
            }
            else if(this.parasitizedPoint.z-this.facMesh.size.z/2<hostFacInfo.parasitizedFace.positionZ-hostFacInfo.parasitizedFace.sizeZ/2){
              this.parasitizedPoint.z=hostFacInfo.parasitizedFace.positionZ-hostFacInfo.parasitizedFace.sizeZ/2+this.facMesh.size.z/2;
            }
            if(this.parasitizedPoint.x+this.facMesh.size.x/2>hostFacInfo.parasitizedFace.positionX+hostFacInfo.parasitizedFace.sizeX/2){
              this.parasitizedPoint.x=hostFacInfo.parasitizedFace.positionX+hostFacInfo.parasitizedFace.sizeX/2-this.facMesh.size.x/2;
            }
            else if(this.parasitizedPoint.x-this.facMesh.size.x/2<hostFacInfo.parasitizedFace.positionX-hostFacInfo.parasitizedFace.sizeX/2){
              this.parasitizedPoint.x=hostFacInfo.parasitizedFace.positionX-hostFacInfo.parasitizedFace.sizeX/2+this.facMesh.size.x/2;
            }
            break;
        
    }
			
			var ctrMax=new THREE.Vector3(this.facMesh.size.x/2+this.parasitizedPoint.x,this.facMesh.size.y/2+this.parasitizedPoint.y,this.facMesh.size.z/2+this.parasitizedPoint.z);
            var ctrMin=new THREE.Vector3(this.parasitizedPoint.x-this.facMesh.size.x/2,this.parasitizedPoint.y-this.facMesh.size.y/2,this.parasitizedPoint.z-this.facMesh.size.z/2);
            for(var i in this.scene.children){
                var g=this.scene.children[i];
            if( g instanceof THREE.Mesh && g.type!='fac' && g.type!='background' && g.type!='cursor' ){
                    var gMax=new THREE.Vector3(g.position.x+g.size.x/2,g.position.y+g.size.y/2,g.position.z+g.size.z/2);
                    var gMin=new THREE.Vector3(g.position.x-g.size.x/2,g.position.y-g.size.y/2,g.position.z-g.size.z/2);
                    if(Math.min(ctrMax.x,gMax.x)>Math.max(ctrMin.x,gMin.x) && 
                       Math.min(ctrMax.y,gMax.y)>Math.max(ctrMin.y,gMin.y) && 
                       Math.min(ctrMax.z,gMax.z)>Math.max(ctrMin.z,gMin.z)) 
                   {
                    // planeGeo=new THREE.CubeGeometry( g.size.x+2,g.size.y+2,g.size.z+2);
                    // planeMat=new THREE.MeshBasicMaterial( { color: 0x000000 } );
                    // this.addMesh(planeGeo,1,g.position.x,g.position.y,g.position.z,0,0,0,planeMat,'tmp');
                    return true;
                    }
                }
            }
      		this.facMesh.position.copy(this.parasitizedPoint);
        	return false;
        
				
			}
    
  },
  move:function(mouse2D){
        // event.preventDefault();
        // mouse2D.x = ( event.clientX / SCREEN_WIDTH ) * 2 - 1;
        // mouse2D.y = - ( event.clientY / SCREEN_HEIGHT  ) * 2 + 1;
        // setTimeout("locate(mouse2D)",0);
        this.locate(mouse2D);
        
  },
  
  locateOnHost:function(mouse2D,hostFacInfo){
    if(!this.facMesh) return;
    
    this.scene.remove(this.scene.getChildByName('tmp'));
        this.mouse2D=mouse2D;
        projector = new THREE.Projector();
        ray = projector.pickingRay(mouse2D.clone(),this.camera );
        //此处应优化为ray.intersect(room );
        var intersects = ray.intersectObjects(this.scene.children );
			this.parasitizedPoint=this.getTouchPointOnHost(intersects,hostFacInfo);
			

        	
			// this.hasInsector();
		
        
  },   
  getTouchPointOnHost:function(intersects,hostFacInfo){
          for( i = 0; i < intersects.length; i++ ) {
          intersector = intersects[ i ];
          
          if(intersector.object.description==this.facMesh.parasitizedFace.name){
            
            return intersector.point;
          }
          
        }
      return null;
  },
  
  
 hasInsector:function(){
 
		var f_vector = new THREE.Vector3( 0, 0, -1 );
        var b_vector = new THREE.Vector3( 0, 0, 1 );
        var l_vector = new THREE.Vector3( -1, 0, 0 );
        var r_vector = new THREE.Vector3( 1, 0, 0 );
        
        halfSize=this.facMesh.size;
        var left = new THREE.Vector3( this.facMesh.position.x-this.facMesh.size.x/2, this.facMesh.position.y-this.facMesh.size.y/2, this.facMesh.position.z );
        var right = new THREE.Vector3( this.facMesh.position.x+this.facMesh.size.x/2, this.facMesh.position.y-this.facMesh.size.y/2, this.facMesh.position.z );
        var front = new THREE.Vector3( this.facMesh.position.x, this.facMesh.position.y-this.facMesh.size.y/2, this.facMesh.position.z+this.facMesh.size.z/2 );
        var back = new THREE.Vector3( this.facMesh.position.x, this.facMesh.position.y-this.facMesh.size.y/2, this.facMesh.position.z-this.facMesh.size.z/2 );


          var left_ray = new THREE.Ray( left, f_vector );
          var left_intersects = left_ray.intersectScene( this.scene );
          if(left_intersects[0].name!='fac'){
          if ( left_intersects.length > 0 && left_intersects[0].distance < this.facMesh.size.z/2 ) {
          	
            this.facMesh.position.z = left_intersects[0].object.position.z+this.facMesh.size.z/2+1;
            console.warn(left_intersects[0].distance);
            return true;
            
          }
         }
        
        
 },
  hasInsector_bak:function(){
        var f_vector = new THREE.Vector3( 0, 0, -1 );
        var b_vector = new THREE.Vector3( 0, 0, 1 );
        var l_vector = new THREE.Vector3( -1, 0, 0 );
        var r_vector = new THREE.Vector3( 1, 0, 0 );
        
        halfSize=this.facMesh.size;
        var left = new THREE.Vector3( this.facMesh.position.x-this.facMesh.size.x/2, this.facMesh.position.y-this.facMesh.size.y/2, this.facMesh.position.z );
        var right = new THREE.Vector3( this.facMesh.position.x+this.facMesh.size.x/2, this.facMesh.position.y-this.facMesh.size.y/2, this.facMesh.position.z );
        var front = new THREE.Vector3( this.facMesh.position.x, this.facMesh.position.y-this.facMesh.size.y/2, this.facMesh.position.z+this.facMesh.size.z/2 );
        var back = new THREE.Vector3( this.facMesh.position.x, this.facMesh.position.y-this.facMesh.size.y/2, this.facMesh.position.z-this.facMesh.size.z/2 );


          var left_ray = new THREE.Ray( left, f_vector );
          var left_intersects = left_ray.intersectScene( this.scene );
          if ( left_intersects.length > 0 && left_intersects[0].distance < this.facMesh.size.z/2 ) {
            this.facMesh.position.z = left_intersects[0].object.position.z+this.facMesh.size.z/2+1;
            console.warn(left_intersects[0].distance);
            return true;
            
          }
          var right_ray = new THREE.Ray( right, f_vector );
          var right_intersects = right_ray.intersectScene( this.scene );
          if ( right_intersects.length > 0 && right_intersects[0].distance < this.facMesh.size.z/2 ) {
            this.facMesh.position.z = right_intersects[0].object.position.z+this.facMesh.size.z/2+1;
            console.warn(left_intersects[0].distance);
            return true;
          }

          var left_ray = new THREE.Ray( left, b_vector );
          var left_intersects = left_ray.intersectScene( this.scene );
          if ( left_intersects.length > 0 && left_intersects[0].distance < this.facMesh.size.z/2 ) {
            this.facMesh.position.z = right_intersects[0].object.position.z-this.facMesh.size.z/2+1;
            return true;
          }
          var right_ray = new THREE.Ray( right, b_vector );
          var right_intersects = right_ray.intersectScene( this.scene );
          if ( right_intersects.length > 0 && right_intersects[0].distance < this.facMesh.size.z/2 ) {
            this.facMesh.position.z = right_intersects[0].object.position.z-this.facMesh.size.z/2-1;
            return true;
         }

          var back_ray = new THREE.Ray( back, r_vector );
          var back_intersects = back_ray.intersectScene( this.scene );
          if ( back_intersects.length > 0 && back_intersects[0].distance < this.facMesh.size.x/2 ) {
            this.facMesh.position.x = back_intersects[0].object.position.x-this.facMesh.size.x/2-1;
            return true;
          }
          var front_ray = new THREE.Ray( front, r_vector );
          var front_intersects = front_ray.intersectScene( this.scene );
          if ( front_intersects.length > 0 && front_intersects[0].distance < this.facMesh.size.x/2 ) {
            this.facMesh.position.x = front_intersects[0].object.position.x-this.facMesh.size.x/2-1;
            return true;
          }

          var back_ray = new THREE.Ray( back, l_vector );
          var back_intersects = back_ray.intersectScene( this.scene );
          if ( back_intersects.length > 0 && back_intersects[0].distance < this.facMesh.size.x/2 ) {
            this.facMesh.position.x = back_intersects[0].object.position.x+this.facMesh.size.x/2+1;
            return true;
          }
          var front_ray = new THREE.Ray( front, l_vector );
          var front_intersects = front_ray.intersectScene( this.scene );
          if ( front_intersects.length > 0 && front_intersects[0].distance < this.facMesh.size.x/2 ) {
            this.facMesh.position.x = front_intersects[0].object.position.x+this.facMesh.size.x/2+1;
            return true;
          }
        
        
  },
  locate:function(mouse2D){
  	if(!this.facMesh) return;
  	
  	this.scene.remove(this.scene.getChildByName('tmp'));
        this.mouse2D=mouse2D;
        projector = new THREE.Projector();
        ray = projector.pickingRay(mouse2D.clone(),this.camera );
        //此处应优化为ray.intersect(room );
        
        var intersects = ray.intersectScene(this.scene );
        intersector = this.getRealIntersector( intersects );
        // cubeRefMesh.position.copy(intersector.point); 
        
          if ( intersector ) {
                              var ctrPosition=this.facMesh.position;
                              var ctrSize=this.facMesh.size;
                              var tchPoint=intersector.point;
                              var tchPosition=intersector.object.position;
                              var targetPoint=new THREE.Vector3();
                              // targetPoint.copy(cubeRefMesh.position);
                              var interSize=intersector.object.size;
                              var tmpPoint=new THREE.Vector3;
                              var checkIntersect=false;
                              tmpPoint.copy(tchPoint);
                              
                              if(interSize.x/2-Math.abs(tchPoint.x-tchPosition.x)<.15){
                                  checkIntersect=this.checkIntersect(tmpPoint,ctrSize,intersector,this.facMesh);
                                  // tchPoint.x=Math.floor( tchPoint.x / interSize.x ) * interSize.x + interSize.x/2;
                                  tmpPoint.x=tchPoint.x;
                                  if(tchPoint.x>tchPosition.x){
                                    //use chceckIntersctX
                                    if(!checkIntersect){
                                        targetPoint.y=tchPoint.y;
                                        targetPoint.z=tchPoint.z;
                                        targetPoint.x=tchPoint.x+ctrSize.x/2;
                                        
                                        this.facMesh.position.copy(targetPoint);
                                    }
                                  }
                                  else{

                                    if(!checkIntersect){
                                        targetPoint.y=tchPoint.y;
                                        targetPoint.z=tchPoint.z;
                                        targetPoint.x=tchPoint.x-ctrSize.x/2;
                                        this.facMesh.position.copy(targetPoint);
                                    }
                                  }

                              }
                                    
                              if(interSize.y/2-Math.abs(tchPoint.y-tchPosition.y)<.15){
                                checkIntersect=this.checkIntersect(tmpPoint,ctrSize,intersector,this.facMesh);
                                  // tchPoint.y=Math.floor( tchPoint.y / interSize.y ) * interSize.y + interSize.y/2;
                                  tmpPoint.y=tchPoint.y;
                                  if(tchPoint.y>tchPosition.y){
                                    
                                    //use chceckIntersctZ
                                    if(!checkIntersect){
                                        targetPoint.y=tchPoint.y+ctrSize.y/2;
                                        targetPoint.z=tchPoint.z;
                                        targetPoint.x=tchPoint.x;
                                        this.facMesh.position.copy(targetPoint);
                                    }
                                    
                                       
                                  }
                                  else{
                                    if(!checkIntersect){
                                        targetPoint.y=tchPoint.y-ctrSize.y/2;
                                        targetPoint.z=tchPoint.z;
                                        targetPoint.x=tchPoint.x;
                                        this.facMesh.position.copy(targetPoint);
                                    }
                                  }

                              } 
                              if(interSize.z/2-Math.abs(tchPoint.z-tchPosition.z)<.15){
                                  // tchPoint.z=Math.floor( tchPoint.z / interSize.z ) * interSize.z + interSize.z/2;
                                  tmpPoint.z=tchPoint.z;
                                  if(tchPoint.z>tchPosition.z){
                                    
                                    //use chceckIntersctZ
                                    if(!checkIntersect){
                                        targetPoint.y=tchPoint.y;
                                        targetPoint.z=tchPoint.z+ctrSize.z/2;
                                        targetPoint.x=tchPoint.x;
                                        this.facMesh.position.copy(targetPoint);
                                    }
                                    
                                       
                                  }
                                  else{
                                    if(!checkIntersect){
                                        targetPoint.y=tchPoint.y;
                                        targetPoint.z=tchPoint.z+ctrSize.z/2;
                                        targetPoint.x=tchPoint.x;
                                        this.facMesh.position.copy(targetPoint);
                                    }
                                  }

                              }      
                                                    
                              
                         }       

    },


  setOnBoard:function(plane,scene,camera){

    projector = new THREE.Projector();
    ray = projector.pickingRay(this.mouse2D.clone(),camera );
        var intersects = ray.intersectScene( this.scene );
      if ( intersects.length > 0 ) {
            intersector = this.getRealIntersector( intersects,plane );
            if ( intersector ) {
               
                this.setPosition( intersector );
               
             }
        }
  },
  getOnBoard:function(mouse2D,plane,scene,camera){
    // mouse3D = this.projector.unprojectVector( mouse2D.clone(), this.camera );
    projector = new THREE.Projector();
    ray = projector.pickingRay(mouse2D.clone(),camera );
        var intersects = ray.intersectObjects( scene.children );
      if ( intersects.length > 0 ) {
      // // intersector = this.getRealIntersector( intersects,plane );
      tor=null;
      for( i = 0; i < intersects.length; i++ ) {
            intersector = intersects[ i ];
             if ( intersector.object.type !="background" && 
                //intersector.object.type!='wall' && 
                //intersector.object.type!='floor'&&
                intersector.object.type !='cursor') {
                     tor= intersector;
                    this.facMesh=tor.object;
                    var today=new Date();
                    //this.facMesh.name=Math.ceil((today.getTime()*9301+49297)%233280/233280.0*511555);
                    if(intersector.object.type=='released'){
                    	this.facMesh.type='fac';
                    	this.goodsInfo={name:this.facMesh.name,type:this.facMesh.type,goodsName:this.facMesh.goodsName,description:this.facMesh.description,parasitizedFace:this.facMesh.parasitizedFace};
                    }else if(intersector.object.type=='wall'){
              			this.goodsInfo={name:this.facMesh.name,type:this.facMesh.type,goodsName:this.facMesh.description,description:this.facMesh.description,parasitizedFace:this.facMesh.parasitizedFace};
                    }else if(intersector.object.type=='floor'){
                    	this.goodsInfo={name:this.facMesh.name,type:this.facMesh.type,goodsName:this.facMesh.description,description:this.facMesh.description,parasitizedFace:this.facMesh.parasitizedFace};
                    }

                    return true; 
                    }
            }
        }
        return false;
  },
  // setPosition:function (intersector ) {
    // var tmpVec = new THREE.Vector3();
    // var voxelPosition = new THREE.Vector3();
        // tmpVec.copy( intersector.face.normal );
        // voxelPosition.add( intersector.point, intersector.object.matrixRotationWorld.multiplyVector3( tmpVec ) );
        // // console.warn(voxelPosition);
//         
    // // the codes below should be checked and tested.
            // // voxelPosition.x = Math.floor( voxelPosition.x / this.centerOffset.z ) * this.centerOffset.z + this.centerOffset.z/2;
            // // voxelPosition.y = Math.floor( voxelPosition.y / this.centerOffset.y ) * this.centerOffset.y + this.centerOffset.y/2;
            // // voxelPosition.z = Math.floor( voxelPosition.z / this.centerOffset.x ) * this.centerOffset.x + this.centerOffset.x/2;
//     
//     
        // voxelPosition.x = Math.floor( voxelPosition.x / this.baseSize ) * this.baseSize + 20;
        // voxelPosition.y = Math.floor( voxelPosition.y / 10 ) * 10 + 5;
        // voxelPosition.z = Math.floor( voxelPosition.z / this.baseSize ) * this.baseSize + 20;
// 
      // if(this.facMesh){
            // if(voxelPosition.x<this.facMesh.position.x){
              // this.keyLeft=true;
              // this.keyRight=false;
            // }else if(voxelPosition.x>this.facMesh.position.x){
              // this.keyRight=true;
              // this.keyLeft=false;
            // }else {
              // this.keyRight=false;
              // this.keyLeft=false;
            // }
            // if(voxelPosition.z<this.facMesh.position.z){
              // this.keyFront=true;
              // this.keyBack=false;
            // }else if(voxelPosition.z>this.facMesh.position.z){
              // this.keyFront=false;
              // this.keyBack=true;
            // }else {
              // this.keyFront=false;
              // this.keyBack=false;
            // }
      // if(voxelPosition.y<this.facMesh.position.y){
              // this.keyUp=true;
              // this.keyDown=false;
            // }else if(voxelPosition.y>this.facMesh.position.y){
              // this.keyUp=false;
              // this.keyDown=true;
//               
            // }else {
              // this.keyUp=false;
              // this.keyDown=false;
            // }
            // // this.control();
//             
//             
            // this.facMesh.position.copy(voxelPosition);
            // // this.control2();
            // // // this.facMesh.position.y=40;
            // // this.facMesh.updateMatrix();
          // }
//           
         // //未作碰撞检测
//                     
          // // this.this.facMesh.position.copy(voxelPosition);
          // // this.this.facMesh.updateMatrix();
     // },
        control:this.control2,
  getRealIntersector:function(intersects,flat){
      for( i = 0; i < intersects.length; i++ ) {
          intersector = intersects[ i ];
          
          if(intersector.object.type!='fac' && intersector.object.type!='background'){

          	return intersector;
          }
          
        }
      return null;
  },
     
  bind:function(){
    this.bind=true;
  },
  release:this.release,
  rotate:function(){
    
  },
  clear:function(){
    this.bind=false;
    scene.remove(this.facMesh);
    this.facMesh=null;
  },
  rand:function(number){

  }
  
}